package com.vf.admin.miaodong.service.impl;

import com.vf.admin.client.service.*;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.dao.AfterFollowDao;
import com.vf.admin.miaodong.entity.*;
import com.vf.admin.miaodong.service.AfterFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 关注
 */
@Service
public class AfterFollowServiceImpl implements AfterFollowService {

    @Autowired
    AfterFollowDao afterFollowDao;
    @Autowired
    AppUserService appUserService;
    @Autowired
    AppRoomService appRoomService;
    @Autowired
    AppFollowService appFollowService;
    @Autowired
    AppTaskRecordService appTaskRecordService;
    @Autowired
    AppTaskService appTaskService;

    /**
     * 查询列表
     */
    @Override
    public List<AppFollow> list(Map<String, Object> paraMap, SplitPageBean splitPage) {
        return afterFollowDao.list(paraMap, splitPage);
    }

    /**
     * 详情
     */
    @Override
    public AppFollow get(Integer id) {
        return afterFollowDao.get(id);
    }

    /**
     * 添加/修改
     */
    @Override
    public void save(AppFollow haoYou) {
        afterFollowDao.save(haoYou);
    }

    /**
     * 删除
     */
    @Override
    public void del(Integer id) {
        afterFollowDao.del(id);
    }

    /**
     *  關注
     * @param userId 用戶ID
     * @param beiguanzhuId 被關注用戶iD
     * @return
     */
    @Override
    @Transactional(rollbackFor = java.lang.Exception.class)
    public String guanzhu(Integer userId, Integer beiguanzhuId) {
        if(userId == null && userId.intValue() <= 0){
            return "用户ID为空";
        }
        if(beiguanzhuId == null && beiguanzhuId.intValue() <= 0){
            return "被关注用户Id为空";
        }
        AppUser appUser = appUserService.get(userId);
        if(StringUtils.isEmpty(appUser)){
            return "系统没有此用户记录";
        }
        AppUser user = appUserService.get(beiguanzhuId);
        if(StringUtils.isEmpty(user)){
            return "被关注用户不存在此系统";
        }
        AppFollow follew = appFollowService.getFollew(userId, beiguanzhuId);
        if(!StringUtils.isEmpty(follew)){
            return "請不要重複關注這個主播！";
        }
        AppRoom appRoom = appRoomService.getUserId(beiguanzhuId);
        if(!StringUtils.isEmpty(appRoom)){
            //如果不为空就代表 被关注的人是主播
            //保存信息到關注表
            AppFollow appFollow = new AppFollow();
            appFollow.setUserid(user.getId());
            appFollow.setBgzusercode(user.getUsercode());
            appFollow.setBgzphone(user.getLoginname());
            appFollow.setBgznick(user.getNick());
            appFollow.setBgzpic(user.getPic());
            appFollow.setBgzgrade(String.valueOf(user.getUserlevel()));
            appFollow.setBgzsex(user.getSex());
            appFollow.setBgzmysign(user.getMysign());

            appFollow.setFolloweduserid(appUser.getId());
            appFollow.setFollowedusercode(appUser.getUsercode());
            appFollow.setFollowedpic(appUser.getPic());
            appFollow.setFollowednick(appUser.getNick());
            appFollow.setFollowedphone(appUser.getLoginname());
            appFollow.setFollowedgrade(String.valueOf(appUser.getUserlevel()));
            appFollow.setFollowedsex(appUser.getSex());
            appFollow.setFollowedmysign(appUser.getMysign());
            appFollow.setStatus(1);
            appFollow.setCreatetime(new Date());
            appFollow.setUpdatetime(new Date());

            appFollowService.save(appFollow);
            /*// 在任務完成詳情裏面加入記錄

            AppTask appTask = appTaskService.getById(9);
            AppTaskRecord appTaskRecord = new AppTaskRecord();
            if (!StringUtils.isEmpty(appTask)){
                appTaskRecord.setUserid(appUser.getId());
                appTaskRecord.setNick(appUser.getNick());
                appTaskRecord.setPic(appUser.getPic());
                appTaskRecord.setTaskname(appTask.getTaskname());
                appTaskRecord.setTaskid(appTask.getId());
                appTaskRecord.setJinbi(appTask.getJinbi());
                appTaskRecord.setLingqu(2);
                appTaskRecord.setCreatetime(new Date());
            }
            appTaskRecordService.saveOrUpdate(appTaskRecord);*/
            return "關注成功！";
        }
        // 走這裏就代表 關注的是普通人 不是主播
        AppFollow appFollow = new AppFollow();
        appFollow.setUserid(user.getId());
        appFollow.setBgzusercode(user.getUsercode());
        appFollow.setBgzphone(user.getLoginname());
        appFollow.setBgznick(user.getNick());
        appFollow.setBgzpic(user.getPic());
        appFollow.setBgzgrade(String.valueOf(user.getUserlevel()));
        appFollow.setBgzsex(user.getSex());
        appFollow.setBgzmysign(user.getMysign());

        appFollow.setFolloweduserid(appUser.getId());
        appFollow.setFollowedusercode(appUser.getUsercode());
        appFollow.setFollowedpic(appUser.getPic());
        appFollow.setFollowednick(appUser.getNick());
        appFollow.setFollowedphone(appUser.getLoginname());
        appFollow.setFollowedgrade(String.valueOf(appUser.getUserlevel()));
        appFollow.setFollowedsex(appUser.getSex());
        appFollow.setFollowedmysign(appUser.getMysign());
        appFollow.setStatus(1);
        appFollow.setCreatetime(new Date());
        appFollow.setUpdatetime(new Date());

        appFollowService.save(appFollow);
        return "關注成功！";
    }

}
